﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace QD.Tools
{
    public static class FileHelper
    {
        /// <summary>
        /// 判断记事本文件是否存在
        /// </summary>
        /// <param name="txtPath"></param>
        /// <returns></returns>
        public static bool Exists(string txtPath)
        {
            return File.Exists(txtPath);
        }

        /// <summary>
        /// 从记事本中讯取内容
        /// </summary>
        /// <returns></returns>
        public static string ReadFromTxt(string txtPath)
        {
            FileStream fs = new FileStream(txtPath, FileMode.OpenOrCreate);
            StreamReader reader = new StreamReader(fs);
            string str = reader.ReadToEnd();
            reader.Close();
            reader.Dispose();

            fs.Close();
            fs.Dispose();

            return str;
        }

        /// <summary>
        /// 追加内容到记事本
        /// </summary>
        public static void AppendToTxt(string txtPath, string context)
        {
            if (context.Length > 0)
            {
                FileStream fs = new FileStream(txtPath, FileMode.Append);
                StreamWriter writer = new StreamWriter(fs);
                writer.WriteLine(context);

                writer.Close();
                writer.Dispose();

                fs.Close();
                fs.Dispose();
            }
        }

        /// <summary>
        /// 添加内容到记事本（会清空记事本中原来的记录）
        /// </summary>
        /// <param name="txtPath"></param>
        /// <param name="context"></param>
        public static void AddToTxt(string txtPath, string context)
        {
            if (context.Length > 0)
            {
                FileStream fs = new FileStream(txtPath, FileMode.Create);
                StreamWriter writer = new StreamWriter(fs);
                writer.WriteLine(context);

                writer.Close();
                writer.Dispose();

                fs.Close();
                fs.Dispose();
            }
        }

        /// <summary>
        /// 删除记事本
        /// </summary>
        /// <param name="txtPath"></param>
        public static void DelTxt(string txtPath)
        {
            if (File.Exists(txtPath))
            {
                File.Delete(txtPath);
            }
        }

        /// <summary>
        /// 清空记事本
        /// </summary>
        /// <param name="txtPath"></param>
        public static void Clear(String txtPath)
        {
            FileStream stream = File.Open(txtPath, FileMode.OpenOrCreate, FileAccess.Write);
            stream.Seek(0, SeekOrigin.Begin);
            stream.SetLength(0);
            stream.Close();
        }

        /// <summary>
        /// 创建目录
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static DirectoryInfo CreateDirectory(string path)
        {
            DirectoryInfo diReturn = null;
            try
            {
                diReturn = Directory.CreateDirectory(path);
            }
            catch (System.IO.DirectoryNotFoundException except)
            {
                string[] pathList = path.Split('\\');
                for (int i = 0; i < pathList.Length; i++)
                {
                    string tempPath = string.Join("\\", pathList, 0, i + 1);
                    if (Directory.Exists(tempPath))
                    {
                        continue;
                    }
                    else
                    {
                        DirectoryInfo diReturnTemp = Directory.CreateDirectory(tempPath);
                        if (i == pathList.Length - 1)
                        {
                            diReturn = diReturnTemp;
                        }
                    }
                }
            }
            return diReturn;
        }
    }
}
